/**
 * Copyright @ 2000 ysten Co. Ltd.
 * All right reserved.
 * @author: wangmin
 * date: 2016-05-13
 */

$(function () {
    var url;
    $('#twoTypeGrid').datagrid({
        singleSelect: true,
        fitColumns: true,
        rownumbers: true,
        pagination: true,
        pageSize: 20,
        shadow: false,
        columns: [
            [
                {title: '<b>主键</b>', field: 'id', align: 'center', width: 50, sort: 'clientSide'},
                {title: '<b>二级分类名称</b>', field: 'name', align: 'center', width: 150, sort: 'clientSide'},
                {title: '<b>二级分类备注</b>', field: 'description', align: 'center', width: 250, sort: 'clientSide'}
            ]
        ]
    });
    $('#oneTypeGrid').datagrid({
        url: 'findOneLevelTypePage.json',
        singleSelect: true,
        fitColumns: true,
        rownumbers: true,
        pagination: true,
        pageSize: 20,
        shadow: false,
        columns: [
            [
                {title: '<b>主键</b>', field: 'id', align: 'center', width: 50, sort: 'clientSide'},
                {title: '<b>一级分类名称</b>', field: 'name', align: 'center', width: 150, sort: 'clientSide'},
                {title: '<b>一级分类备注</b>', field: 'description', align: 'center', width: 250, sort: 'clientSide'}
            ]
        ],
        onClickRow: function (index, row) {
            $('#oneTypeId').val(row.id);
            $('#twoTypeGrid').datagrid({url: 'findTwoLevelTypePage.json',queryParams:{"search_EQ_oneLevelTypeId":row.id}});
        },
        onLoadSuccess: function (data) {
            if (data.rows.length > 0) {
                $('#oneTypeId').val(data.rows[0].id);
                $('#twoTypeGrid').datagrid({url: 'findTwoLevelTypePage.json',queryParams:{"search_EQ_oneLevelTypeId":data.rows[0].id}});
            } else {
                $('#oneTypeId').val(null);
            }
        }
    });

    $("#queryOneTypeBtn").click(function () {
        $('#oneTypeGrid').datagrid('load', {
            search_LIKE_name: $("#searchOneName").textbox("getValue"),
            search_LIKE_description: $("#searchOneDescription").textbox("getValue")
        });
    });

    $("#queryTwoBtn").click(function () {
        $('#twoTypeGrid').datagrid('load', {
            search_LIKE_name: $("#searchTwoName").textbox("getValue"),
            search_LIKE_description: $("#searchTwoDescription").textbox("getValue"),
            search_EQ_oneLevelTypeId : $("#oneTypeId").val()
        });
    });

    $("#addOneType").click(function () {
        $('#dlg-one').dialog({
            title: '新增一级分类',
            modal: true
        });
        $('#dlg-one').dialog('open').dialog('center');
        $('#fm-one').form('clear');
        url = 'saveOneLevelType.json';
    });

    $("#editOneType").click(function(){
        var rows = $('#oneTypeGrid').datagrid('getSelections');
        if (rows && rows.length == 1) {
            $('#fm-one').form('clear');
            $.get("findOneLevelTypeById.json?id=" + rows[0].id, function (data) {
                if(data){
                    $('#dlg-one').dialog('open').dialog('center').dialog('setTitle', '修改一级分类');
                    $('#fm-one').form('load', data);
                }
            });
            url = 'updateOneLevelType.json';
        } else {
            EGIFT_APP.showEditWarnFunc();
        }
    });

    $("#submitOne").click(function(){
        $('#fm-one').form('submit', {
            url: url,
            onSubmit: function () {
                var result = $(this).form('validate');
                if(result) {
                    $('#dlg-one').dialog('close');
                }
                return result;
            },
            success: function (data) {
                var obj = JSON.parse(data);
                if (obj.result) {
                    $.messager.show({
                        title: '成功',
                        msg: obj.msg
                    });
                } else {
                    $.messager.show({
                        title: '失败',
                        msg: obj.msg
                    });
                }
                $('#oneTypeGrid').datagrid('reload');
            }
        });
    });

    $("#deleteOneType").click(function(){
        var rows = $('#oneTypeGrid').datagrid('getSelections');
        if (rows && rows.length > 0) {
            $.messager.confirm('确认', '确认要删除吗？', function (r) {
                if (r) {
                    $.post('deleteOneLevelType.json', {id: rows[0].id}, function (result) {
                        if (result) {
                            $.messager.show({
                                title: '成功',
                                msg: "删除一级分类成功"
                            });
                            $('#oneTypeGrid').datagrid('reload');
                        } else {
                            $.messager.show({
                                title: '失败',
                                msg: "删除一级分类失败"
                            });
                        }
                    });
                }
            });
        } else {
            EGIFT_APP.showDeleteWarnFunc();
        }
    });

    $("#addTwoType").click(function () {
        $('#dlg-two').dialog({
            title: '新增二级分类',
            modal: true
        });
        $('#dlg-two').dialog('open').dialog('center');
        $('#fm-two').form('clear');
        $("#oneLevelTypeId").val($("#oneTypeId").val());
        url = 'saveTwoLevelType.json';
    });

    $("#editTwoType").click(function(){
        var rows = $('#twoTypeGrid').datagrid('getSelections');
        if (rows && rows.length == 1) {
            $('#fm-two').form('clear');
            $.get("findTwoLevelTypeById.json?id=" + rows[0].id, function (data) {
                if(data){
                    $('#dlg-two').dialog('open').dialog('center').dialog('setTitle', '修改二级分类');
                    $('#fm-two').form('load', data);
                }
            });
            url = 'updateTwoLevelType.json';
        } else {
            EGIFT_APP.showEditWarnFunc();
        }
    });

    $("#submitTwo").click(function(){
        $('#fm-two').form('submit', {
            url: url,
            onSubmit: function () {
                var result = $(this).form('validate');
                if(result) {
                    $('#dlg-two').dialog('close');
                }
                return result;
            },
            success: function (data) {
                var obj = JSON.parse(data);
                if (obj.result) {
                    $.messager.show({
                        title: '成功',
                        msg: obj.msg
                    });
                } else {
                    $.messager.show({
                        title: '失败',
                        msg: obj.msg
                    });
                }
                $('#twoTypeGrid').datagrid('reload');
            }
        });
    });

    $("#deleteTwoType").click(function(){
        var rows = $('#twoTypeGrid').datagrid('getSelections');
        if (rows && rows.length > 0) {
            $.messager.confirm('确认', '确认要删除吗？', function (r) {
                if (r) {
                    $.post('deleteTwoLevelType.json', {id: rows[0].id}, function (result) {
                        if (result == "success") {
                            $.messager.show({
                                title: '成功',
                                msg: "删除二级分类成功"
                            });
                            $('#twoTypeGrid').datagrid('reload');
                        } else {
                            $.messager.show({
                                title: '失败',
                                msg: "删除二级分类失败"
                            });
                        }
                    });
                }
            });
        } else {
            EGIFT_APP.showDeleteWarnFunc();
        }
    });
})
